// pages/my/mySet/ewm/ewm.js
import { Data } from '../../../../utils/data.js';
const app = getApp()
const CONFIG = require('../../../../config.js')
const WXAPI = require('../../../../wxapi/main')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src: 'http://mmbiz.qpic.cn/mmbiz_png/icTdbqWNOwNTTiaKet81gQJDXYnPiaJFSzRlp9frTTX2hSN01xhiackVLHHrG7ZQI3XQsbM7Gr9USZdN4f26SO5xjg/0?wx_fmt=png',
    info: '',
  name:'李慧慧',
  dizhi:'江西 南昌'
  },
// ---------------------
  getImageInfo() {
    wx.getImageInfo({
      src: this.data.src,
      complete: (res) => {
        console.log(res)
        console.log(res.path)
        // 返回的是json对象
        this.setData({
          info: this.format(res)
        })
      }
    })
  },
  //格式化json对象，打印成字符串格式
  format(obj) {
    // Object.keys(obj)
    // Object.keys 返回一个所有元素为字符串的数组
    // Object.keys(obj).map
    // map() 方法创建一个新数组
    // 使用 map 重新格式化数组中的对象
    return '{\n' +
      Object.keys(obj).map(function (key) {
        return '  ' + key + ': ' + obj[key] + ','
      }).join('\n') + '\n' +
      '}'
  },
  previewImage: function (e) {
    console.log('previewImage')
    wx.previewImage({
      current: this.data.canvasUrl, // 当前显示图片的http链接   
      urls: [this.data.canvasUrl] // 需要预览的图片http链接列表   
    })
  },



// ----------------------
  showTopTips(){
    wx.navigateTo({
      url: "../card/card"
    })
  },
  onLoad() {

  },
  onShow() {
    let that = this;
    let userInfo = wx.getStorageSync('userInfo')
    if (!userInfo) {
      app.goLoginPageTimeOut()
    } else {
      that.setData({
        userInfo: userInfo,
        version: CONFIG.version,
        vipLevel: app.globalData.vipLevel
      })
    }
    this.getUserApiInfo();
    this.getUserAmount();
  },

  getPhoneNumber: function (e) {
    if (!e.detail.errMsg || e.detail.errMsg != "getPhoneNumber:ok") {
      wx.showModal({
        title: '提示',
        content: '无法获取手机号码:' + e.detail.errMsg,
        showCancel: false
      })
      return;
    }
    var that = this;
    WXAPI.bindMobile({
      token: wx.getStorageSync('token'),
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv
    }).then(function (res) {
      if (res.code === 10002) {
        app.goLoginPageTimeOut()
        return
      }
      if (res.code == 0) {
        wx.showToast({
          title: '绑定成功',
          icon: 'success',
          duration: 2000
        })
        that.getUserApiInfo();
      } else {
        wx.showModal({
          title: '提示',
          content: '绑定失败',
          showCancel: false
        })
      }
    })
  },
  getUserApiInfo: function () {
    var that = this;
    WXAPI.userDetail(wx.getStorageSync('token')).then(function (res) {
      if (res.code == 0) {
        let _data = {}
        _data.apiUserInfoMap = res.data
        if (res.data.base.mobile) {
          _data.userMobile = res.data.base.mobile
        }
        that.setData(_data);
      }
    })
  },
  getUserAmount: function () {
    var that = this;
    WXAPI.userAmount(wx.getStorageSync('token')).then(function (res) {
      if (res.code == 0) {
        that.setData({
          balance: res.data.balance.toFixed(2),
          freeze: res.data.freeze.toFixed(2),
          score: res.data.score
        });
      }
    })
  },
  relogin: function () {
    app.goLoginPageTimeOut()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})